package com.hellobike.haxing.modules.wms.controller;

import java.util.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONObject;
import com.hellobike.haxing.common.vo.Result;
import com.hellobike.haxing.common.system.query.QueryGenerator;
import com.hellobike.haxing.modules.supplier.entity.TSupplierInfo;
import com.hellobike.haxing.modules.supplier.service.ITSupplierInfoService;
import com.hellobike.haxing.modules.wms.dto.ReturnSupplierHeaderDto;
import com.hellobike.haxing.modules.wms.entity.ReturnSupplierHeader;
import com.hellobike.haxing.modules.wms.exception.WmsServiceException;
import com.hellobike.haxing.modules.wms.facade.WmsReturnFacade;
import com.hellobike.haxing.modules.wms.service.IReturnSupplierHeaderService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import com.hellobike.haxing.common.annotation.AutoLog;
 /**
 * @Description: t_wms_return_supplier_header
 * @Author: haxing-boot
 * @Date:   2020-02-11
 * @Version: V1.0
 */
@RestController
@RequestMapping("/wms/returnSupplierHeader")
@Slf4j
public class ReturnSupplierHeaderController extends WmsBaseController<ReturnSupplierHeader, IReturnSupplierHeaderService> {
	@Autowired
	private IReturnSupplierHeaderService returnSupplierHeaderService;
	@Autowired
	private WmsReturnFacade wmsReturnFacade;
	
	/**
	 * 分页列表查询
	 *
	 * @param returnSupplierHeader
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "t_wms_return_supplier_header-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(ReturnSupplierHeader returnSupplierHeader,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<ReturnSupplierHeader> queryWrapper = QueryGenerator.initQueryWrapper(returnSupplierHeader, req.getParameterMap());
		Page<ReturnSupplierHeader> page = new Page<ReturnSupplierHeader>(pageNo, pageSize);
		IPage<ReturnSupplierHeader> pageList = returnSupplierHeaderService.page(page, queryWrapper);
		IPage<ReturnSupplierHeaderDto> result = wmsReturnFacade.wrapperPageSupplierHeader(pageList);
		return Result.ok(result);
	}
	
	/**
	 *   添加
	 *
	 * @param param
	 * @return
	 */
	@AutoLog(value = "t_wms_return_supplier_header-添加",operateType=2)
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody JSONObject param) throws WmsServiceException {
		ReturnSupplierHeader header = param.getObject("header",ReturnSupplierHeader.class);
		List<Long> ids = param.getJSONArray("ids").toJavaList(Long.class);
		wmsReturnFacade.addReturnSupplierOrder(header,ids,getWarehouseId(),getSysUserId());
		return Result.ok("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param returnSupplierHeader
	 * @return
	 */
	@AutoLog(value = "t_wms_return_supplier_header-编辑",operateType=3)
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody ReturnSupplierHeader returnSupplierHeader) {
		returnSupplierHeaderService.updateById(returnSupplierHeader);
		return Result.ok("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "t_wms_return_supplier_header-删除",operateType=4)
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		returnSupplierHeaderService.removeById(id);
		return Result.ok("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "t_wms_return_supplier_header-批量删除",operateType=4)
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.returnSupplierHeaderService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.ok("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		ReturnSupplierHeader returnSupplierHeader = returnSupplierHeaderService.getById(id);
		if(returnSupplierHeader==null) {
			return Result.error("未找到对应数据");
		}
		return Result.ok(returnSupplierHeader);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param returnSupplierHeader
    */
    @AutoLog(value = "t_wms_return_supplier_header-导出excel",operateType=6)
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, ReturnSupplierHeader returnSupplierHeader) {
        return super.exportXls(request, returnSupplierHeader, ReturnSupplierHeader.class, "t_wms_return_supplier_header");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @AutoLog(value = "t_wms_return_supplier_header-导入excel",operateType=5)
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, ReturnSupplierHeader.class);
    }


	 @Autowired
	 private ITSupplierInfoService supplierInfoService;

	 @GetMapping(value = "/getSuppliers")
	 public Result<?> getSuppliers() {
		 List<TSupplierInfo> suppliers = supplierInfoService.getBaseMapper().selectList(new QueryWrapper<>());
		 return Result.ok(suppliers);
	 }

}
